package com.wlm.shortlink.admin.controller;

import com.wlm.shortlink.admin.common.convention.result.Result;
import com.wlm.shortlink.admin.common.convention.result.Results;
import com.wlm.shortlink.admin.dto.req.UserLoginReqDTO;
import com.wlm.shortlink.admin.dto.req.UserRegisterReqDTO;
import com.wlm.shortlink.admin.dto.req.UserUpdateReqDTO;
import com.wlm.shortlink.admin.dto.resp.UserLoginRespDTO;
import com.wlm.shortlink.admin.dto.resp.UserRespDTO;
import com.wlm.shortlink.admin.service.UserService;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

/**
 * 用户管理控制层
 */
@RestController
@RequiredArgsConstructor
@RequestMapping("/api/short-link/admin/v1")
public class UserController {

    private final UserService userService;

    /**
     * 根据用户名获取用户信息
     */
    @GetMapping("/user/{username}")
    public Result<UserRespDTO> getUserByUsername(@PathVariable("username") String username) {
        UserRespDTO result = userService.getUserByUsername(username);
        return Results.success(result);
    }

    /**
     * 查询用户名是否存在
     */
    @GetMapping("/user/has-username/{username}")
    public Result<Boolean> hasUsername(@PathVariable("username") String username) {
        System.out.println("username: " + username);
        Boolean result = userService.hasUsername(username);
        System.out.println(result);
        return Results.success(result);
    }

    /**
     * 注册用户
     */
    @PostMapping("/user")
    public Result<Void> register(@RequestBody UserRegisterReqDTO requestParam) {
        userService.Register(requestParam);
        return Results.success();
    }

    /**
     * 修改用户信息
     */
    @PutMapping("/user")
    public Result<Void> update(@RequestBody UserUpdateReqDTO requestParam) {
        userService.update(requestParam);
        return Results.success();
    }

    /**
     * 用户登陆
     */
    @PostMapping("/user/login")
    public Result<UserLoginRespDTO> login(@RequestBody UserLoginReqDTO requestParam) {
        return Results.success(userService.login(requestParam));
    }

    /**
     * 检查用户是否登陆
     */
    @GetMapping("/user/check-login")
    public Result<Boolean> checkLogin(@RequestParam("username") String username,@RequestParam("token") String token){
        return Results.success(userService.checkLogin(username,token));
    }

    /**
     * 用户登出
     */
    @DeleteMapping("/user/logout")
    public Result<Void> logout(@RequestParam("username") String username,@RequestParam("token") String token){
        // TODO 用户登出
        userService.logout(username,token);
        return Results.success();
    }
}
